<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Config;
use App\Models\Order;
use App\Models\User;
use Illuminate\Http\Request;

class VipOrderController extends BaseController
{

    public function index(Request $request)
    {
        $input = $request->input();
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');
        $order_sn = $request->input('order_sn');
        $nickname = $request->input('nickname');
        $status = $request->input('status', -1);
        $product_type = $request->input('product_type', Order::PRODUCT_TYPE_VIP);
        $benefit_user = $input['benefit_user'] ?? User::BENEFIT_DAREN;

        $list = Order::orderSn($order_sn)->where('product_type', $product_type)->startAt($start_at)->endAt($end_at)
          ->when($nickname, function ($q) use($nickname){
                $q->whereHas('user', function ($qr) use($nickname) {
                    $qr->where('nickname', 'like', '%'.$nickname.'%');
                });
            })
            ->when($benefit_user, function ($q) use($benefit_user) {
                $q->where('benefit_user', $benefit_user);
            })
            ->status($status)
            ->withCertain('user.merchant', ['contact'])
            ->orderBy('id', 'desc')
            ->paginate(per_page());

        $info = [];
        if ($product_type == Order::PRODUCT_TYPE_DAREN) {
            $info['daren_amount'] = Config::getDarenAmount();
        }
        return $this->paginate($list, $info);
    }

}

